package com.ruoyi.com.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.com.domain.ComBusinessClassify;
import com.ruoyi.com.domain.ComBusinessClassifyContent;
import com.ruoyi.com.mapper.ComBusinessClassifyContentMapper;
import com.ruoyi.common.core.domain.OperateBasicForm;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.com.mapper.ComBusinessContentMapper;
import com.ruoyi.com.domain.ComBusinessContent;
import com.ruoyi.com.service.IComBusinessContentService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 业务管理-内容发布Service业务层处理
 *
 * @author sunye
 * @date 2024-12-07
 */
@Service
public class ComBusinessContentServiceImpl extends ServiceImpl<ComBusinessContentMapper, ComBusinessContent> implements IComBusinessContentService
{
    @Autowired
    private ComBusinessContentMapper comBusinessContentMapper;

    @Autowired
    private ComBusinessClassifyContentMapper comBusinessClassifyContentMapper;

    /**
     * 查询业务管理-内容发布
     *
     * @param contentId 业务管理-内容发布主键
     * @return 业务管理-内容发布
     */
    @Override
    public ComBusinessContent selectComBusinessContentByContentId(Long contentId)
    {
        ComBusinessContent comBusinessContent = comBusinessContentMapper.selectComBusinessContentByContentId(contentId);
        if(comBusinessContent !=null){
            List<Long> list = comBusinessClassifyContentMapper.selectComBusinessClassifyContentByContentId(contentId);
            comBusinessContent.setClassifyIdList(list);
        }
        return comBusinessContent;
    }

    /**
     * 查询业务管理-内容发布列表
     *
     * @param comBusinessContent 业务管理-内容发布
     * @return 业务管理-内容发布
     */
    @Override
    public List<ComBusinessContent> selectComBusinessContentList(ComBusinessContent comBusinessContent)
    {
        return comBusinessContentMapper.selectComBusinessContentList(comBusinessContent);
    }

    @Override
    public List<ComBusinessContent> getListByClassifyId(ComBusinessContent comBusinessContent) {
        return comBusinessContentMapper.getListByClassifyId(comBusinessContent);
    }

    /**
     * 新增业务管理-内容发布
     *
     * @param comBusinessContent 业务管理-内容发布
     * @return 结果
     */
    @Override
    @Transactional
    public int insertComBusinessContent(ComBusinessContent comBusinessContent)
    {
        List<Long> classifyIdList = comBusinessContent.getClassifyIdList();
        Date nowDate = DateUtils.getNowDate();
        comBusinessContent.setCreateTime(nowDate);
        comBusinessContent.setUpdateTime(nowDate);
        comBusinessContent.setPublishStatus(1);
        comBusinessContent.setPublishTime(nowDate);
        int insert = comBusinessContentMapper.insertComBusinessContent(comBusinessContent);
        if(insert>0){
            if(classifyIdList !=null && classifyIdList.size()>0){
                List<ComBusinessClassifyContent> list = new ArrayList<>();
                Long contentId = comBusinessContent.getContentId();
                Long deptId = comBusinessContent.getDeptId();
                for (Long classifyId : classifyIdList) {
                    ComBusinessClassifyContent businessClassifyContent = new ComBusinessClassifyContent();
                    businessClassifyContent.setClassifyId(classifyId);
                    businessClassifyContent.setContentId(contentId);
                    businessClassifyContent.setDeptId(deptId);
                    list.add(businessClassifyContent);
                }
                comBusinessClassifyContentMapper.batchInsertComBusinessClassifyContent(list);
            }
        }
        return insert;
    }

    /**
     * 修改业务管理-内容发布
     *
     * @param comBusinessContent 业务管理-内容发布
     * @return 结果
     */
    @Override
    @Transactional
    public int updateComBusinessContent(ComBusinessContent comBusinessContent)
    {
        List<Long> classifyIdList = comBusinessContent.getClassifyIdList();
        comBusinessContent.setUpdateTime(DateUtils.getNowDate());
        int update = comBusinessContentMapper.updateComBusinessContent(comBusinessContent);
        if(update > 0){
            Long contentId = comBusinessContent.getContentId();
            Long deptId = comBusinessContent.getDeptId();
            //删除分类内容关联
            comBusinessClassifyContentMapper.deleteComBusinessClassifyContentByContentId(contentId);
            //新增分类内容关联
            if(classifyIdList !=null && classifyIdList.size()>0){
                List<ComBusinessClassifyContent> list = new ArrayList<>();
                for (Long classifyId : classifyIdList) {
                    ComBusinessClassifyContent businessClassifyContent = new ComBusinessClassifyContent();
                    businessClassifyContent.setClassifyId(classifyId);
                    businessClassifyContent.setContentId(contentId);
                    businessClassifyContent.setDeptId(deptId);
                    list.add(businessClassifyContent);
                }
                comBusinessClassifyContentMapper.batchInsertComBusinessClassifyContent(list);
            }
        }
        return update;
    }

    /**
     * 批量删除业务管理-内容发布
     *
     * @param contentIds 需要删除的业务管理-内容发布主键
     * @return 结果
     */
    @Override
    public int deleteComBusinessContentByContentIds(Long[] contentIds)
    {
        return comBusinessContentMapper.deleteComBusinessContentByContentIds(contentIds);
    }

    /**
     * 删除业务管理-内容发布信息
     *
     * @param contentId 业务管理-内容发布主键
     * @return 结果
     */
    @Override
    public int deleteComBusinessContentByContentId(Long contentId)
    {
        return comBusinessContentMapper.deleteComBusinessContentByContentId(contentId);
    }

    /**
     * 置为有效/无效业务管理-内容发布
     *
     */
    @Override
    public int effectiveStatus(OperateBasicForm operateBasicForm)
    {
        return comBusinessContentMapper.effectiveStatus(operateBasicForm);
    }


}
